/**
 * SpringMVC restful 风格
 * 用户控制器，控制关于用户的一些业务逻辑操作
 * @author yanwl
 * @date 2015-03-20
 */
package com.maven.demo.controller;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;


import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;


/**
 * 控制器注解 请求映射注解 controller 代表类是控制器 restcontroller 代表类是控制器， 所有的方法 都是ajax方法
 * 
 * @author yanwl
 *
 */
@Controller
@RequestMapping("/upload")
public class UploadController {
	
	/**
	 * @return 用户列表页面
	 * @throws Exception
	 */
	@RequestMapping(value = "", method = RequestMethod.GET)
	public ModelAndView index() throws Exception {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("upload");
		return mav;
	}
	
	
	@RequestMapping(value = "/upload.htm", method = RequestMethod.POST)
	@ResponseBody
	public String upload(HttpServletRequest request) throws IOException{
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile(request.getParameter("fileName"));
		if(file != null && !file.isEmpty()) {
			float fileSize = Float.valueOf(file.getSize());// 返回文件大小，单位为k
			String extend = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1).toLowerCase();
			String saveFileName = UUID.randomUUID().toString() + "." + extend;
			//String saveFilePathName = "F:/images";
			String saveFilePathName = System.getProperty("user.home") + File.separator + "images";
			System.out.println(saveFilePathName);
			File path = new File(saveFilePathName);
			if(!path.exists()) {
				path.mkdir();
			}
			DataOutputStream out = new DataOutputStream(new FileOutputStream(saveFilePathName + File.separator + saveFileName));
			InputStream is = null;
			try {
				is = file.getInputStream();
				int size = (int)fileSize;
				byte[] buffer = new byte[size];
				while(is.read(buffer) > 0) {
					out.write(buffer);
				}
			}catch(IOException e) {
				e.printStackTrace();
			}finally {
				if(is != null) {
					is.close();
				}
				if(out != null) {
					out.close();
				}
			}
			
			return JSON.toJSONString(saveFileName);
		}
		return null;
	}
	
	@RequestMapping(value = "/many/upload.htm", method = RequestMethod.POST)
	@ResponseBody
	public void many_upload(HttpServletRequest request) throws IOException{
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Map<String, MultipartFile> fileMaps = multipartRequest.getFileMap();
		if(fileMaps.size() > 0 && !fileMaps.isEmpty()) {
			String saveFilePathName = System.getProperty("user.home") + File.separator + "images";
			System.out.println(saveFilePathName);
			File path = new File(saveFilePathName);
			if(!path.exists()) {
				path.mkdir();
			}
			for (Map.Entry<String, MultipartFile> entity : fileMaps.entrySet()) {
				//上传文件
				MultipartFile mf = entity.getValue();
				String extend = mf.getOriginalFilename().substring(mf.getOriginalFilename().lastIndexOf(".") + 1).toLowerCase();
				String fileName = UUID.randomUUID().toString() + "." + extend;
				System.out.println("name:"+fileName);
				File file = new File(saveFilePathName + File.separator + fileName);
				try {
					FileCopyUtils.copy(mf.getBytes(), file);
					System.out.println("上传成功");
				}catch(IOException e) {
					System.out.println("上传失败");
					e.printStackTrace();
				}
			}
		}
	}
}
